package com.zrc.superframe.activity

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ColorUtils
import com.zrc.superframe.BR
import com.zrc.superframe.R
import com.zrc.superframe.databinding.ActivityMainBinding
import com.zrc.superframe.viewmodel.MainVM
import com.zrc.superframe_base.ARouterHub
import com.zrc.superframe_base.mvx.mvvm.BaseMvvmActivity
import com.zrc.superframe_ui.layout.SuperViewPagerAdapter
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
@Route(path = ARouterHub.ACTIVITY_MAIN, name = "主页面")
class MainActivity : BaseMvvmActivity<ActivityMainBinding, MainVM>() {

    lateinit var adapter: SuperViewPagerAdapter

    override fun getVariableId() = BR.viewModel

    override fun getLayoutId() = R.layout.activity_main

    override fun getViewModel() = MainVM()

    override fun initObserver() {
        super.initObserver()
        adapter = SuperViewPagerAdapter(supportFragmentManager)
        val fragment1=ARouter.getInstance().build(ARouterHub.FRAGMENT_MAIN).navigation() as Fragment
        adapter.addFragment(fragment1, "首页", R.mipmap.index1, R.mipmap.index)
        val fragment2=ARouter.getInstance().build(ARouterHub.FRAGMENT_FIND).navigation() as Fragment
        adapter.addFragment(fragment2, "发现", R.mipmap.find1, R.mipmap.find)
        val fragment3=ARouter.getInstance().build(ARouterHub.FRAGMENT_MOMENTS).navigation() as Fragment
        adapter.addFragment(fragment3, "消息", R.mipmap.message1, R.mipmap.message)
        val fragment4=ARouter.getInstance().build(ARouterHub.FRAGMENT_MY).navigation() as Fragment
        adapter.addFragment(fragment4, "我的", R.mipmap.me1, R.mipmap.me)
        mBinding.viewPager.adapter=adapter
        mBinding.viewPager.offscreenPageLimit = adapter.count//fragment缓存设置
        initMagicIndicator1()
//        val netFragment = ARouter.getInstance().build(ARouterHub.FRAGMENT_DEMO1).navigation() as BaseMvvmFragment<*, *>
//        loadRootFragment(R.id.ll_content,netFragment)
    }

    private fun initMagicIndicator1() {
        val commonNavigator = CommonNavigator(this)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {

            override fun getCount()=adapter.count

            override fun getTitleView(context: Context, index: Int): IPagerTitleView? {
                val commonPagerTitleView = CommonPagerTitleView(context)
                val customLayout: View = LayoutInflater.from(context).inflate(R.layout.layout_bottom_item, null)
                val titleImg = customLayout.findViewById<View>(R.id.title_img) as ImageView
                val titleText = customLayout.findViewById<View>(R.id.title_text) as TextView
                titleImg.setImageResource(adapter.getOnIcon(index))
                titleText.text = adapter.getPageTitle(index)
                commonPagerTitleView.setContentView(customLayout)
                commonPagerTitleView.onPagerTitleChangeListener = object : CommonPagerTitleView.OnPagerTitleChangeListener {
                    override fun onSelected(index: Int, totalCount: Int) {
                        titleImg.setImageResource(adapter.getOnIcon(index))
                        titleText.setTextColor(ColorUtils.getColor(R.color.gray_2c2c2c))
                    }

                    override fun onDeselected(index: Int, totalCount: Int) {
                        titleImg.setImageResource(adapter.getUpIcon(index))
                        titleText.setTextColor(ColorUtils.getColor(R.color.gray_707070))
                    }

                    override fun onLeave(index: Int, totalCount: Int, leavePercent: Float, leftToRight: Boolean) {
                        titleImg.scaleX = 1.3f + (0.8f - 1.3f) * leavePercent
                        titleImg.scaleY = 1.3f + (0.8f - 1.3f) * leavePercent
                    }

                    override fun onEnter(index: Int, totalCount: Int, enterPercent: Float, leftToRight: Boolean) {
                        titleImg.scaleX = 0.8f + (1.3f - 0.8f) * enterPercent
                        titleImg.scaleY = 0.8f + (1.3f - 0.8f) * enterPercent
                    }
                }
                commonPagerTitleView.setOnClickListener {
                    mBinding.viewPager.currentItem = index
                }
                return commonPagerTitleView
            }

            override fun getIndicator(context: Context?): IPagerIndicator?=null
        }
        mBinding.magicIndicator1.navigator = commonNavigator
        ViewPagerHelper.bind(mBinding.magicIndicator1, mBinding.viewPager)
    }
}